package com.zoe.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoe.app.dao.SysUserMapper;
import com.zoe.app.entity.SysUser;
import com.zoe.app.event.ZoeTransactionEvent;
import com.zoe.app.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zbg
 * @date 2024/02/22
 * @description $description
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveTransaction(SysUser sysUser) {
        log.info(this.toString());

        // 在这里提交也没事，因为是事务提交成功后才会触发事件
        applicationContext.publishEvent(new ZoeTransactionEvent(this, sysUser));
        Object o = AopContext.currentProxy();
        log.info("aop: " + o.toString());
        this.save(sysUser);
        // this.save(sysUser);
        // System.out.println(1/0);
        return true;
    }
}
